package com.ruoyi.kjy.controller;/**
 * @InterfaceName: BannerController
 * @Description:
 * @Author: YourName
 * @Date: 2023-04-19
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.kjy.domain.Banner;
import com.ruoyi.kjy.service.BannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * @InterfaceName: BannerController
 * @Description:
 * @Author: lilirui
 * @Date: 2023-04-19
 */
@Api(tags = "轮播图")
@RestController
@RequestMapping("/banner")
public class BannerController extends BaseController {
    @Autowired
    private BannerService bannerService;

    @GetMapping("/list")
    public AjaxResult list(Long current, Long size, Banner banner) {
        QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(banner);
        if (Objects.isNull(current)) {
            current = 1L;
        }
        if (Objects.isNull(size)) {
            size = 10L;
        }
        if (size >= 20L) {
            size = 20L;
        }
        return AjaxResult.success(bannerService.page(new Page<>(current, size), queryWrapper));
    }

    @Log(title = "科技成果管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Banner banner) {
        QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(banner);
        List<Banner> list = bannerService.list(queryWrapper);
        ExcelUtil<Banner> util = new ExcelUtil<Banner>(Banner.class);
        util.exportExcel(response, list, "科技成果管理数据");
    }

    @ApiOperation("详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bannerService.getById(id));
    }

    /**
     * 新增科技成果管理
     */
    @ApiOperation("发布")
    @Log(title = "科技成果管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Banner banner) {
        return toAjax(bannerService.save(banner));
    }

    /**
     * 修改科技成果管理
     */
    @Log(title = "科技成果管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Banner banner) {
        return toAjax(bannerService.updateById(banner));
    }

    /**
     * 删除科技成果管理
     */
    @Log(title = "科技成果管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bannerService.removeById(ids));
    }
}
